<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.7/dist/vue.js"></script>
</head>
<body>

<div id="app">
    <p id="totalCount">参与投票的人次: {{ totalcount }} </p>
</div>

<script>
    var vm = new Vue({
        el: '#app',
        data: {
            totalcount: '统计中...'
        },
        mounted:function(){
            //前端发送websocket连接请求
            var webSocketUrl = "ws://localhost:8080/vote/" + Math.random();
            var websocket = new WebSocket( webSocketUrl );
            //只要后端通过websocket向此连接发消息就会触发onmessage事件
            websocket.onmessage = function(event){
                var msg = event.data;
                vm.totalcount = msg;
            }
        }
    });
</script>
</body>
</html>
